package com.szewec.ps.myview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.szewec.ps.R;
import com.szewec.ps.util.Constants;
import com.szewec.ps.myinterface.VoidCallBackInterface;

public class SearchTitleBar extends RelativeLayout {
    protected RelativeLayout titleLayout;
    protected RelativeLayout rlSearch;
    protected EditText etSearch;
    protected TextView tvSearch;
    protected ImageView ivClear;
    protected RelativeLayout leftLayout;
    protected RelativeLayout rightLayout;
    protected ImageView rightImage;
    protected TextView rightText;

    public SearchTitleBar(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public SearchTitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public SearchTitleBar(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        LayoutInflater.from(context).inflate(R.layout.search_title_bar, this);
        titleLayout = findViewById(R.id.root);
        rlSearch = findViewById(R.id.rl_search);
        etSearch = findViewById(R.id.et_search);
        tvSearch = findViewById(R.id.tv_search);
        ivClear = findViewById(R.id.iv_clear);
        leftLayout = findViewById(R.id.left_layout);
        rightLayout = findViewById(R.id.right_layout);
        rightImage = findViewById(R.id.right_image);
        rightText = findViewById(R.id.right_text);

        //监听删除按钮的显示隐藏
        initListener();
        parseStyle(context, attrs);
    }

    private void parseStyle(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SearchTitleBar);
            String searchType = ta.getString(R.styleable.SearchTitleBar_searchType);
            Drawable rd = ta.getDrawable(R.styleable.SearchTitleBar_rightImage);
            String rt = ta.getString(R.styleable.SearchTitleBar_rightText);
            //默认显示左边
            if(Constants.SearchType.SEARCH_1.equals(searchType)){
                leftLayout.setVisibility(GONE);//左边不显示
                rightLayout.setVisibility(VISIBLE);//右边显示
                if (null != rd) {
                    rightImage.setImageDrawable(rd);
                    rightImage.setVisibility(VISIBLE);
                }
                if (null != rt) {
                    rightText.setText(rt);
                    rightText.setVisibility(VISIBLE);
                }
            }else if(Constants.SearchType.SEARCH_2.equals(searchType)){
                rightLayout.setVisibility(VISIBLE);//两边都显示
                if (null != rd) {
                    rightImage.setImageDrawable(rd);
                    rightImage.setVisibility(VISIBLE);
                }
                if (null != rt) {
                    rightText.setText(rt);
                    rightText.setVisibility(VISIBLE);
                }
            }
            ta.recycle();
        }
    }

    public void setEditorActionListener(TextView.OnEditorActionListener listener){
        etSearch.setOnEditorActionListener(listener);
    }

    public void setLeftLayoutClickListener(VoidCallBackInterface callBack) {
        leftLayout.setOnClickListener(v -> callBack.work());
    }

    public void setRightLayoutClickListener(VoidCallBackInterface callBack) {
        rightLayout.setOnClickListener(v -> callBack.work());
    }

    public void setTextChangedListener(VoidCallBackInterface callBack) {
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String text = s.toString();
                if (!etSearch.isFocused() && text.length() <= 0) {
                    //只有当输入框失去焦点且输入框没有输入文本的时候，才显示提示文本
                    tvSearch.setVisibility(View.VISIBLE);
                } else {
                    tvSearch.setVisibility(View.GONE);
                }
                if (etSearch.isFocused() && text.length() > 0) {
                    //只有当前输入框获取到焦点以及输入框的内容不为空才显示删除按钮
                    ivClear.setVisibility(VISIBLE);
                } else {
                    ivClear.setVisibility(View.GONE);
                }
                callBack.work();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    public void setRightTextVisibility(int visibility) {
        rightText.setVisibility(visibility);
    }

    public void setRightText(String mRightText) {
        rightText.setText(mRightText);
    }

    public void setEtSearchFocus(){
        etSearch.setText("");
        etSearch.setFocusable(true);
        etSearch.setFocusableInTouchMode(true);
        etSearch.requestFocus();
        InputMethodManager imm = (InputMethodManager) etSearch.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_NOT_ALWAYS);
    }

    public EditText getEtSearch() {
        return etSearch;
    }

    public void setKeyWord(String keyWord){
        if(keyWord != null){
            etSearch.setText(keyWord);
        }
    }

    public String getKeyWord() {
        if (etSearch.getText() != null) {
            return etSearch.getText().toString();
        }
        return "";
    }

    /**
     * 初始化各种监听事件， 如监听删除按钮的显示/隐藏等
     */
    private void initListener() {
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String text = s.toString();
                if (!etSearch.isFocused() && text.length() <= 0) {
                    //只有当输入框失去焦点且输入框没有输入文本的时候，才显示提示文本
                    tvSearch.setVisibility(View.VISIBLE);
                } else {
                    tvSearch.setVisibility(View.GONE);
                }
                if (etSearch.isFocused() && text.length() > 0) {
                    //只有当前输入框获取到焦点以及输入框的内容不为空才显示删除按钮
                    ivClear.setVisibility(VISIBLE);
                } else {
                    ivClear.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        etSearch.setOnFocusChangeListener((v, hasFocus) -> {
            String text = etSearch.getText().toString();
            if (!hasFocus && text.length() <= 0) {
                //只有当输入框失去焦点且输入框没有输入文本的时候，才显示提示文本
                tvSearch.setVisibility(View.VISIBLE);
            } else {
                tvSearch.setVisibility(View.GONE);
            }
            if (hasFocus && text.length() > 0) {
                //只有当前输入框获取到焦点以及输入框的内容不为空才显示删除按钮
                ivClear.setVisibility(View.VISIBLE);
            } else {
                ivClear.setVisibility(View.GONE);
            }
        });
        //清空输入框文本事件
        ivClear.setOnClickListener(v -> etSearch.setText(""));
    }
}
